


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > ScorePanel</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.ui</a>
</div>

<h1>Coverage Summary for Class: ScorePanel (nl.tudelft.jpacman.ui)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ScorePanel</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/25)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.ui;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.GridLayout;
<i class="no-highlight">4</i>&nbsp;import java.util.LinkedHashMap;
<i class="no-highlight">5</i>&nbsp;import java.util.List;
<i class="no-highlight">6</i>&nbsp;import java.util.Map;
<i class="no-highlight">7</i>&nbsp;
<i class="no-highlight">8</i>&nbsp;import javax.swing.JLabel;
<i class="no-highlight">9</i>&nbsp;import javax.swing.JPanel;
<i class="no-highlight">10</i>&nbsp;
<i class="no-highlight">11</i>&nbsp;import nl.tudelft.jpacman.level.Player;
<i class="no-highlight">12</i>&nbsp;
<i class="no-highlight">13</i>&nbsp;/**
<i class="no-highlight">14</i>&nbsp; * A panel consisting of a column for each player, with the numbered players on
<i class="no-highlight">15</i>&nbsp; * top and their respective scores underneath.
<i class="no-highlight">16</i>&nbsp; *
<i class="no-highlight">17</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">18</i>&nbsp; *
<i class="no-highlight">19</i>&nbsp; */
<b class="nc"><i class="no-highlight">20</i>&nbsp;public class ScorePanel extends JPanel {</b>
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;    /**
<i class="no-highlight">23</i>&nbsp;     * Default serialisation ID.
<i class="no-highlight">24</i>&nbsp;     */
<i class="no-highlight">25</i>&nbsp;    private static final long serialVersionUID = 1L;
<i class="no-highlight">26</i>&nbsp;
<i class="no-highlight">27</i>&nbsp;    /**
<i class="no-highlight">28</i>&nbsp;     * The map of players and the labels their scores are on.
<i class="no-highlight">29</i>&nbsp;     */
<i class="no-highlight">30</i>&nbsp;    private final Map&lt;Player, JLabel&gt; scoreLabels;
<i class="no-highlight">31</i>&nbsp;
<i class="no-highlight">32</i>&nbsp;    /**
<i class="no-highlight">33</i>&nbsp;     * The default way in which the score is shown.
<i class="no-highlight">34</i>&nbsp;     */
<b class="nc"><i class="no-highlight">35</i>&nbsp;    public static final ScoreFormatter DEFAULT_SCORE_FORMATTER =</b>
<b class="nc"><i class="no-highlight">36</i>&nbsp;        (Player player) -&gt; String.format(&quot;Score: %3d&quot;, player.getScore());</b>
<i class="no-highlight">37</i>&nbsp;
<i class="no-highlight">38</i>&nbsp;    /**
<i class="no-highlight">39</i>&nbsp;     * The way to format the score information.
<i class="no-highlight">40</i>&nbsp;     */
<b class="nc"><i class="no-highlight">41</i>&nbsp;    private ScoreFormatter scoreFormatter = DEFAULT_SCORE_FORMATTER;</b>
<i class="no-highlight">42</i>&nbsp;
<i class="no-highlight">43</i>&nbsp;    /**
<i class="no-highlight">44</i>&nbsp;     * Creates a new score panel with a column for each player.
<i class="no-highlight">45</i>&nbsp;     *
<i class="no-highlight">46</i>&nbsp;     * @param players
<i class="no-highlight">47</i>&nbsp;     *            The players to display the scores of.
<i class="no-highlight">48</i>&nbsp;     */
<i class="no-highlight">49</i>&nbsp;    public ScorePanel(List&lt;Player&gt; players) {
<b class="nc"><i class="no-highlight">50</i>&nbsp;        super();</b>
<b class="nc"><i class="no-highlight">51</i>&nbsp;        assert players != null;</b>
<i class="no-highlight">52</i>&nbsp;
<b class="nc"><i class="no-highlight">53</i>&nbsp;        setLayout(new GridLayout(2, players.size()));</b>
<i class="no-highlight">54</i>&nbsp;
<b class="nc"><i class="no-highlight">55</i>&nbsp;        for (int i = 1; i &lt;= players.size(); i++) {</b>
<b class="nc"><i class="no-highlight">56</i>&nbsp;            add(new JLabel(&quot;Player &quot; + i, JLabel.CENTER));</b>
<i class="no-highlight">57</i>&nbsp;        }
<b class="nc"><i class="no-highlight">58</i>&nbsp;        scoreLabels = new LinkedHashMap&lt;&gt;();</b>
<b class="nc"><i class="no-highlight">59</i>&nbsp;        for (Player player : players) {</b>
<b class="nc"><i class="no-highlight">60</i>&nbsp;            JLabel scoreLabel = new JLabel(&quot;0&quot;, JLabel.CENTER);</b>
<b class="nc"><i class="no-highlight">61</i>&nbsp;            scoreLabels.put(player, scoreLabel);</b>
<b class="nc"><i class="no-highlight">62</i>&nbsp;            add(scoreLabel);</b>
<b class="nc"><i class="no-highlight">63</i>&nbsp;        }</b>
<i class="no-highlight">64</i>&nbsp;    }
<i class="no-highlight">65</i>&nbsp;
<i class="no-highlight">66</i>&nbsp;    /**
<i class="no-highlight">67</i>&nbsp;     * Refreshes the scores of the players.
<i class="no-highlight">68</i>&nbsp;     */
<i class="no-highlight">69</i>&nbsp;    protected void refresh() {
<b class="nc"><i class="no-highlight">70</i>&nbsp;        for (Map.Entry&lt;Player, JLabel&gt; entry : scoreLabels.entrySet()) {</b>
<b class="nc"><i class="no-highlight">71</i>&nbsp;            Player player = entry.getKey();</b>
<b class="nc"><i class="no-highlight">72</i>&nbsp;            String score = &quot;&quot;;</b>
<b class="nc"><i class="no-highlight">73</i>&nbsp;            if (!player.isAlive()) {</b>
<b class="nc"><i class="no-highlight">74</i>&nbsp;                score = &quot;You died. &quot;;</b>
<i class="no-highlight">75</i>&nbsp;            }
<b class="nc"><i class="no-highlight">76</i>&nbsp;            score += scoreFormatter.format(player);</b>
<b class="nc"><i class="no-highlight">77</i>&nbsp;            entry.getValue().setText(score);</b>
<b class="nc"><i class="no-highlight">78</i>&nbsp;        }</b>
<i class="no-highlight">79</i>&nbsp;    }
<i class="no-highlight">80</i>&nbsp;
<i class="no-highlight">81</i>&nbsp;    /**
<i class="no-highlight">82</i>&nbsp;     * Provide means to format the score for a given player.
<i class="no-highlight">83</i>&nbsp;     */
<i class="no-highlight">84</i>&nbsp;    public interface ScoreFormatter {
<i class="no-highlight">85</i>&nbsp;
<i class="no-highlight">86</i>&nbsp;        /**
<i class="no-highlight">87</i>&nbsp;         * Format the score of a given player.
<i class="no-highlight">88</i>&nbsp;         * @param player The player and its score
<i class="no-highlight">89</i>&nbsp;         * @return Formatted score.
<i class="no-highlight">90</i>&nbsp;         */
<i class="no-highlight">91</i>&nbsp;        String format(Player player);
<i class="no-highlight">92</i>&nbsp;    }
<i class="no-highlight">93</i>&nbsp;
<i class="no-highlight">94</i>&nbsp;    /**
<i class="no-highlight">95</i>&nbsp;     * Let the score panel use a dedicated score formatter.
<i class="no-highlight">96</i>&nbsp;     * @param scoreFormatter Score formatter to be used.
<i class="no-highlight">97</i>&nbsp;     */
<i class="no-highlight">98</i>&nbsp;    public void setScoreFormatter(ScoreFormatter scoreFormatter) {
<b class="nc"><i class="no-highlight">99</i>&nbsp;        assert scoreFormatter != null;</b>
<b class="nc"><i class="no-highlight">100</i>&nbsp;        this.scoreFormatter = scoreFormatter;</b>
<i class="no-highlight">101</i>&nbsp;    }
<i class="no-highlight">102</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
